<?php
namespace app\jobmanagement\controller;


use think\Request;
use app\jobmanagement\service\Common as CommonService;
use app\jobmanagement\service\Task as TaskService;

class Task extends Base
{
    protected $sTask;

    protected $sCommon;


    public function __construct()
    {
        $this->sTask = new TaskService();
        $this->sCommon = new CommonService();
    }

    /**
     * zlq
     * 获取不同任务单列表对应参数
     */
    public function getTaskListParam()
    {
        $type = input('type');
        if (!$type) {
            return $this->apiError("参数不正确,请重试");
        }
        $data = $this->sTask->getTaskListParam($type);
        return $this->apiSuccess('success', $data);
    }

    /**
     * zlq
     * 获取待审核任务单列表
     */
    public function getTaskWaitAuditList()
    {
        $param['user'] = input('user');
        $param['date'] = input('date');
        $param['page'] = input('page', 1);
        $param['status'] = input('status');
        $task_list = $this->sTask->getTaskListByType(1, $param);
        return $this->apiSuccess('success', $task_list);
    }

    /******************* 待审核任务单页面功能 start ********************/

    /**
     * @auther zlq
     * @create_time 2020/5/19 14:27
     * @description 待审核任务单页面进行相关审核
     */
    public function taskAuditCheck()
    {
        $task_id  = input('task_id');
        if (!$task_id) {
            return $this->apiError('请求失败,请重试');
        }
        $type = input('type', 1);
        $remark = input('remark', '');
        $number = input('num', 0);
        if ($type == 1) {
            $task = $this->sTask->taskAuditCheckPass($task_id, $remark, $number);
            switch ($task) {
                case 1:
                    return $this->apiSuccess('审核通过任务单成功');
                    break;
                case 0:
                    return $this->apiError('审核通过任务单失败,请重试');
                    break;
                case -1:
                    return $this->apiError('任务单参数不正确,请重试');
                    break;
                case -2:
                    return $this->apiError('任务单信息不正确,请重试');
                    break;
                case -3:
                    return $this->apiError('产品信息不正确,请重试');
                    break;
                case -4:
                    return $this->apiError('产品数量不正确,不能小于1,请重试');
                    break;
                case -5:
                    return $this->apiError('产品数量小数位不能超过4位,请重试');
                    break;
                case -6:
                    return $this->apiError('工人信息不正确,请重试');
                    break;
                default:
                    return $this->apiError('审核通过任务单失败,请重试');
            }
        } else {
            $task = $this->sTask->taskAuditCheckRefuse($task_id, $remark);
            switch ($task) {
                case 1:
                    return $this->apiSuccess('审核退回任务单成功');
                    break;
                default:
                    return $this->apiError('审核退回任务单失败,请重试');
            }
        }

    }
    /******************* 待审核任务单页面功能 end   ********************/

    /**
     * zlq
     * 获取所有任务单列表
     */
    public function getTaskAllList()
    {
        $param['user'] = input('user');
        $param['date'] = input('date');
        $param['page'] = input('page', 1);
        $param['status'] = input('status');
        $task_list = $this->sTask->getTaskListByType(2, $param);
        return $this->apiSuccess('success', $task_list);
    }

    /**
     * zlq
     * 获取我提交的任务单列表
     */
    public function getTaskMyList()
    {
        $param['user'] = input('user');
        $param['date'] = input('date');
        $param['page'] = input('page', 1);
        $param['status'] = input('status');
        $task_list = $this->sTask->getTaskListByType(3, $param);
        return $this->apiSuccess('success', $task_list);
    }
    /**************************提交的任务单列表 相关功能 start***************************/

    /**
     * @auther zlq
     * @create_time 2020/5/19 15:22
     * @description 添加任务单 提供商品列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function taskAdd()
    {
        $prod_list = $this->sCommon->getProductList();
        $task_type = config('dic.task_type');
        $type = [];
        if (!empty($task_type)) {
            foreach ($task_type as $em => $item) {
                $type[] = [
                    'id' => $em,
                    'name' => $item
                ];
            }
        }
        return $this->apiSuccess('success', ['prod' => $prod_list, 'type' => $type]);
    }

    /**
     * @auther zlq
     * @create_time 2020/7/28 16:17
     * @description 获取计费对应产品
     * @param Request $request
     * @param CommonService $common
     * @return \think\Collection|\think\model\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function taskProduct(Request $request, CommonService $common)
    {
        $param['type'] = (int)input('type', 0);
        $prod_list = $this->sCommon->getProductList($param);
        return $this->apiSuccess('success', $prod_list);
    }

    /**
     * @auther zlq
     * @create_time 2020/7/28 15:55
     * @description 获取某产品对应的阶梯计费
     * @param Request $request
     * @param CommonService $common
     * @return \think\Collection|\think\model\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getProductLadder(Request $request, CommonService $common)
    {
        $prod_id = $request->param('prod_id');
        $data = $common->getProductLadder($prod_id);
        return $this->apiSuccess('success', $data);
    }

    /**
     * @auther zlq
     * @create_time 2020/5/19 20:34
     * @description 保存任务单
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function taskSave()
    {
        // 任务时间
        $task_date = input('task_date', '');
        if (!$task_date) {
            return $this->apiError('任务日期不正确, 请重试');
        }
        // 产品ID
        $prod_id = input('prod_id', 0);
        if (!$prod_id) {
            return $this->apiError('任务产品不正确, 请重试');
        }
        // 产品数量
        $number = input('number', 0);
        if (!$number) {
            return $this->apiError('任务产品数量不正确, 请重试');
        }
        // 计费方式
        $type = (int)input('type', 0);
        if (!$type) {
            return $this->apiError('计费方式不正确, 请重试');
        }
        // 计费方式为3是必须要有对应的产品阶梯
        $ladder = (int)input('ladder');
        $ladder_list = input('ladder_list');
        if (($ladder == 3)) {
            if (!$ladder) {
                return $this->apiError('产品对应的阶梯计费不正确');
            }
            if (!$ladder_list) {
                return $this->apiError('工人信息不正确, 请重试');
            }
            $ladder_list = json_decode($ladder_list, true);
        } else {
            // 工种
            $work = input('work', '');
            if (!$work) {
                return $this->apiError('工种信息不正确, 请重试');
            }
        }
        // 判断产品数量是否为正整数 - 根据单位判断，万件可以为小数
        if(!is_numeric($number) ||  $number <= 0){
            return $this->apiError('任务产品数量不正确, 请重试');
        }

        $work_array = json_decode($work, true);
        // 备注
        $remark = input('remark', '');
        $param_array = [
            'task_date' => $task_date,
            'prod_id' => $prod_id,
            'number' => $number,
            'work' => $work_array,
            'remark' => $remark,
            'type' => $type,
            'ladder' => $ladder,
            'ladder_list' => $ladder_list
        ];
        $res = $this->sTask->taskSave($param_array);
        switch ($res) {
            case 1:
                return $this->apiSuccess('任务单提交成功');
                break;
            case -1:
                return $this->apiError('产品数量不正确,请重试');
                break;
            case -2:
                return $this->apiError('任务单日期不正确,请重试');
                break;
            case -3:
                return $this->apiError('产品信息不正确,请重试');
                break;
            case -10:
                return $this->apiError('工种信息不完整,请重试');
                break;
            case -11:
                return $this->apiError('工种所占比例不正确,请重试');
                break;
            case -12:
                return $this->apiError('工种比例之和超过100,请重试');
                break;
            case -13:
                return $this->apiError('工种信息不正确,请重试');
                break;
            case -14:
                return $this->apiError('工种信息与产品不匹配,请重试');
                break;
            case -15:
                return $this->apiError('工种重复添加工人,请重试');
                break;
            case -16:
                return $this->apiError('工人信息不正确,请重试');
                break;
            case -17:
                return $this->apiError('工种对应工人为空,请重试');
                break;
            case -18:
                return $this->apiError('工种比例之和不为100%,请重试');
                break;
            case -19:
                return $this->apiError('工种信息不正确');
                break;
            case -20:
                return $this->apiError('产品数量小数位不能超过4位,请重试');
                break;
            case -21:
                return $this->apiError('商品阶梯价格不正确,请重试');
                break;
            case -22:
                return $this->apiError('商品阶梯价格信息不正确,请重试');
                break;
            case -23:
                return $this->apiError('商品阶梯价格信息不能使用,请重试');
                break;
            case -24:
                return $this->apiError('组员及对应生产数量与总生产数不匹配,请重试');
                break;
            case -25:
                return $this->apiError('组员及对应生产数量不正确,请重试');
                break;
            case -26:
                return $this->apiError('组员生产数量不正确,请重试');
                break;
            case -27:
                return $this->apiError('组员生产数量超出产品阶梯,请重试');
                break;
            case -28:
                return $this->apiError('生产信息不正确,请重试');
                break;
            default:
                return $this->apiError('任务单提交失败,请重试');
                break;
        }
    }


    /**
     * @auther zlq
     * @create_time 2020/5/19 14:39
     * @description 组长确认申请任务单产品生产
     */
    public function taskConfirmProduce()
    {
        $task_id  = input('task_id');
        if (!$task_id) {
            return $this->apiError('请求失败,请重试');
        }
        $res = $this->sTask->taskConfirmProduce($task_id);
        switch ($res) {
            case 1:
                return $this->apiSuccess('确认生产审核已提交');
                break;
            case -1:
                return $this->apiError('参数不正确,请重试');
                break;
            case -2:
                return $this->apiError('任务单信息不正确,请重试');
                break;
            case -3:
                return $this->apiError('任务单状态不正确,请重试');
                break;
            default:
                return $this->apiError('确认生产审核提交失败,请重试');
                break;
        }
    }

    /**
     * @auther zlq
     * @create_time 2020/5/19 14:42
     * @description 申请结算任务单
     */
    public function settleTask()
    {
        $task_id  = input('task_id');
        if (!$task_id) {
            return $this->apiError('请求失败,请重试');
        }
        $res = $this->sTask->settleTask($task_id);
        switch ($res) {
            case 1:
                return $this->apiSuccess('申请结算审核已提交成功');
                break;
            case -1:
                return $this->apiError('参数不正确,请重试');
                break;
            case -2:
                return $this->apiError('任务单信息不正确,请重试');
                break;
            case -3:
                return $this->apiError('任务单状态不正确,请重试');
                break;
            default:
                return $this->apiError('申请结算审核提交失败,请重试');
                break;
        }
    }

    /**************************提交的任务单列表 相关功能   end***************************/

    /**
     * zlq
     * 获取我提交的任务单列表
     */
    public function getTaskWithMeList()
    {
        $param['user'] = input('user');
        $param['date'] = input('date');
        $param['page'] = input('page', 1);
        $param['status'] = input('status');
        $task_list = $this->sTask->getTaskListByType(4, $param);
        return $this->apiSuccess('success', $task_list);
    }

    /**
     * @auther        : zlq
     * @create_time   : 2020/5/19 11:47
     * @description   : 获取任务单详情
     */
    public function getTaskInfo()
    {
        $task_id  = input('task_id');
        if (!$task_id) {
            return $this->apiError('请求失败,请重试');
        }
        $task = $this->sTask->getTaskInfo($task_id);
        if (empty($task)) {
            return $this->apiError('获取任务单失败,请重试');
        } else {
            return $this->apiSuccess('success', $task);
        }
    }


}